/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.service.order;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.persistence.Parameter;
import com.neusoft.lohas.common.service.BaseService;
import com.neusoft.lohas.modules.ivm.dao.order.UserAddressDao;
import com.neusoft.lohas.modules.ivm.entity.order.UserAddress;

/**
 * 商品单位Service
 * @author Jackie.Liu
 * @version 2014-10-30
 */
@Component
@Transactional(readOnly = true)
public class UserAddressService extends BaseService {

	@Autowired
	private UserAddressDao userAddressDao;
	
	public UserAddress get(String id) {
		return userAddressDao.get(id);
	}
	
	public Page<UserAddress> find(Page<UserAddress> page, UserAddress userAddress) {
		DetachedCriteria dc = userAddressDao.createDetachedCriteria();
		dc.add(Restrictions.eq(UserAddress.FIELD_DEL_FLAG, UserAddress.DEL_FLAG_NORMAL));
		dc.add(Restrictions.eq("user.id", userAddress.getUser().getId()));
		dc.addOrder(Order.asc("updateDate"));
		return userAddressDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(UserAddress userAddress) {
		userAddressDao.save(userAddress);
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		userAddressDao.deleteById(id);
	}

	@Transactional(readOnly = false)
	public void updateDefaultFlag(String userId) {
		userAddressDao.updateDefaultFlag(userId);
	}
	
	public UserAddress getByNameAndPhone(String name, String phone) {
		return userAddressDao.getByNameAndPhone(name, phone);
	}
	
}
